<?php 
// 会议 审核发送邮件
/**
参数列表：
    	$param['mid'] = $mid;
    	$param['email'] = $email;
    	$param['title'] = $title;
    	$param['content'] = $content;
 */

//**************************************通用环境定义开始************************//
set_time_limit(0);
ini_set("memory_limit", -1);
define ('APP', dirname(dirname(__FILE__)));

require_once APP . '/frame/Route.php';
require_once FRAME_ROOT_PATH . '/db/Mysql.php';
require_once APP . '/application/configs/application.php';
require_once APP . '/application/models/mail/class.phpmailer.php';

global $cfg;
Mysql::setInstance("default", $cfg['db']['host'], $cfg['db']['user'], $cfg['db']['password'], $cfg['db']['name'], $cfg['db']['charset']);
if ($cfg ['debug'])
    Mysql::getInstance()->setProfiler(true);

$db = Mysql::getInstance();
$str_args = $argv[1];
$arr_args = unserialize(base64_decode($str_args));

//clog(print_R($arr_args, true));
//**************************************定义结束************************//

// 获取发送用户列表

// 获取会议信息
$arr_meet = $db->fetchRow("select * from meet where mid=?", array($arr_args['mid']));


// 处理抄送地址
$arr_cc_mail = array();
$arr = explode(";", $arr_args['ccmail']);
if (is_array($arr)){
	foreach($arr as $v){
		$arr_cc_mail[] = trim($v);
	}
}

$mail = new PHPMailer();

//clog(print_R($arr_meet, true));
$arr_mail_log = array();
$arr_mail_log['type'] = 'check';

$arr_mail_log['sendargs'] = array();
$arr_mail_log['sendargs']['smtpserver'] = $arr_meet['smtpserver'];
$arr_mail_log['sendargs']['email'] = $arr_meet['email'];
$arr_mail_log['sendargs']['emailpwd'] = $arr_meet['emailpwd'];
$arr_mail_log['sendargs']['emailuser'] = $arr_meet['emailuser'];

$arr_mail_log['sendargs'] = serialize($arr_mail_log['sendargs']);

$arr_mail_log['title'] = $arr_args['title'];
$arr_mail_log['mid'] = $arr_args['mid'];

$mail->SMTPDebug = true;
$mail->SMTPKeepAlive = true;
$mail->IsSMTP();					// 启用SMTP
$mail->Host = $arr_meet['smtpserver'];			//SMTP服务器
$mail->SMTPAuth = true;					//开启SMTP认证
$mail->Username = $arr_meet['email'];			// SMTP用户名
$mail->Password = $arr_meet['emailpwd'];				// SMTP密码
$mail->From = $arr_meet['email'];;			//发件人地址
$mail->FromName = $arr_meet['emailuser'];;				//发件人

$mail->WordWrap = 80;					//设置每行字符长度

$mail->CharSet = "utf-8";				// 这里指定字符集！
$mail->Encoding = "base64";
$mail->IsHTML(true);					// 是否HTML格式邮件

$mail->Subject = $arr_args['title'];			//邮件主题

$mail->Body = $arr_args['content'];
$mail->ClearAddresses();
$mail->AddAddress($arr_args['email']);
foreach($arr_cc_mail as $address)
	$mail->AddCC($address);
// 发送成功
$ret = "ok";
try{
	if(!$mail->Send()){
		$ret = $mail->ErrorInfo;
	}
}catch(Exception $e){
	$ret = $e->getMessage();
}	

// 记录发送日志
$arr_mail_log['sendtime'] = date("Y-m-d H:i:s", time());
$arr_mail_log['content'] = $arr_args['content'];
$arr_mail_log['toemail'] = $arr_args['email'];
$arr_mail_log['message'] = $ret;
if (!empty($str_args['ccmail'])){
	$arr_mail_log['toemail'] .= ' 抄送：' . $arr_args['ccmail'];
}
	
$db->insert("mailog", $arr_mail_log);

$mail->SmtpClose();


function clog($text)
{
	file_put_contents(APP . "/cache/a.log", $text, FILE_APPEND);
}
?>